package com.xbdun.service.bmp.facade;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xbdun.common.api.Client;
import com.xbdun.common.api.XbdUnStringResult;
import com.xbdun.common.exceptions.BizException;
import com.xbdun.common.util.CommonUtil;
import com.xbdun.facade.bmp.exceptions.BmpBizException;
import com.xbdun.facade.bmp.request.system.DepartRequest;
import com.xbdun.facade.bmp.result.system.TreeNodeResult;
import com.xbdun.facade.bmp.service.system.DepartFacade;
import com.xbdun.service.bmp.service.system.DepartService;

import lombok.extern.slf4j.Slf4j;

//机构部门
@Component
@Slf4j
public class DepartFacadeImpl implements DepartFacade{
	@Autowired
	DepartService departService;
	public XbdUnStringResult addDepart(DepartRequest request, Client client) {
		XbdUnStringResult result = new XbdUnStringResult();
		try {
			log.info("start - 新增部门初始参数,{}", request);
			
			CommonUtil.validateClient(client);
			checkedAddDepart(request);
			departService.addXbdunDepart(request,client);
		} catch (Exception ex) {
			result.setBizException(ex);
			log.error("新增部门失败,入参:{},异常:{}", request, ex);
		} finally {
			log.info("end - 新增部门结果:{}", result);
		}
		return result;
	}
	
	public XbdUnStringResult modifyDepart(DepartRequest request, Client client){
		XbdUnStringResult result = new XbdUnStringResult();
		try {
			log.info("start - 修改部门初始参数,{}", request);
			
			CommonUtil.validateClient(client);
			checkedModifyDepart(request);
			departService.updateById(request,client);
		} catch (Exception ex) {
			result.setBizException(ex);
			log.error("修改部门失败,入参:{},异常:{}", request, ex);
		} finally {
			log.info("end - 修改部门结果:{}", result);
		}
		return result;
	}

	public TreeNodeResult queryTreeDepart(Client client) {
		TreeNodeResult result = new TreeNodeResult();
		try{
			log.info("start - 获取部门初始参数,{}", client);
			
			CommonUtil.validateClient(client);
			result.setData(departService.queryTreeDepart(client.getOrgCode()));
		} catch (Exception ex) {
			result.setBizException(ex);
			log.error("获取部门失败,入参:{},异常:{}", client, ex);
		} finally {
			log.info("end - 获取部门结果:{}", result);
		}
		return result;
	}
	
	public XbdUnStringResult delDepartByCode(Long id,Client client){
		XbdUnStringResult result = new XbdUnStringResult();
		try{
			log.info("start - 删除部门入参:Code{},客户端:{}",id,client);
			if(null==id||id.longValue()<=0){
				throw BmpBizException.PARAM_CHKE_FAIL.newInstance("删除部门编号不合法");
			}
			CommonUtil.validateClient(client);
			departService.delDepartByCode(id, client);
		} catch (Exception ex) {
			result.setBizException(ex);
			log.error("删除部门失败,code["+id+"],入参:{},异常:{}", client, ex);
		} finally {
			log.info("end - 删除部门结果:{}", result);
		}
		return result;
	}
	
	//检测新增部门数据
	private void checkedAddDepart(DepartRequest request) {
		if (null == request){
			throw BizException.PARAM_CHKE_FAIL.newInstance("请求对象不能为空");
		}
		StringBuilder sb = new StringBuilder();
		if (StringUtils.isBlank(request.getDepartName())) {
			sb.append("部门名称不能为空;");
		}
		if(((request.getCode()+"").length()/8)==2){
			sb.append("部门只能存在3级;");
		}
		if (sb.length() > 0) {
			throw BizException.PARAM_CHKE_FAIL.newInstance("%s", sb.toString());
		}
	}
	//检测修改部门参数
	private void checkedModifyDepart(DepartRequest request) {
		if (null == request){
			throw BizException.PARAM_CHKE_FAIL.newInstance("请求对象不能为空");
		}
		StringBuilder sb = new StringBuilder();
		if (StringUtils.isBlank(request.getDepartName())&&StringUtils.isBlank(request.getRemark())) {
			sb.append("部门名称和备注不能同时为空;");
		}
		if(request.getCode()<=0){
			sb.append("部门编号能为空;");
		}
		if (sb.length() > 0) {
			throw BizException.PARAM_CHKE_FAIL.newInstance("%s", sb.toString());
		}
	}
}
